﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Globalization;
using System.Windows.Forms;

using Elegant.Ui;

namespace iPdf
{
    static class Global
    {
        public const int WMF_FORMAT = 35;
        public const int EXIF_FORMAT = 36;
        public const int EMF_FORMAT = 37;
        //The timer in milliseconds to do the operation
        public static readonly int OperationTimeOut = 10 * 60 * 1000; //10 minutes
        public static readonly string PdfCreatorName = "PDFCreator";
        public static readonly string PdfCreatorDriverName = "PDFCreator";
        public static readonly string NoPdfCreator = "No PDF Creator";
        public static readonly string CustomFormName = "Custom Paper Size";
        public static readonly string PdfExt = ".pdf";
        public static readonly string TiffExt = ".tif";
        public static readonly string BmpExt = ".bmp";
        public static readonly string JpegExt = ".jpg";
        public static readonly string XmlExt = ".xml";
        public static readonly float BookmarkSpecificOffset = 10;
        public static readonly float TextLinkSpecificOffset = 3;
        public static readonly int SizeOfDWordInBytes = 4;
        public static readonly int SizeOfByteInBits = 8;
        public static readonly int SizeOfDwordInBits = SizeOfDWordInBytes * SizeOfByteInBits;
        public static readonly int SizeOfWordInBytes = 2;
        public static readonly int SizeOfWordInBits = SizeOfWordInBytes * SizeOfByteInBits;
        public const int SessionPanelCaptionSize = 25;

        public static object NullObject = System.Reflection.Missing.Value;
        public static object TrueValue = true;
        public static object FalseValue = false;
        public static readonly string Newline = "\r\n";
        public static readonly int RetryTimes = 5;
        public static readonly int RetryInterval = 1; //1s
        public static readonly int RestTime = 1; //1s
        public static readonly int TimeToWaitForChangesToTakeEffects = 1; //1s

        public static readonly string SessionPassword = "Tatapass";

        public static string Tata = "Tata";
        

#if LITE
        public static readonly string ApplicationName = global::iPdf.Properties.Resources.LiteEditionAppName;
#else
        public static readonly string ApplicationName = global::iPdf.Properties.Resources.ProEditionAppName;
#endif
        public static readonly string ProcessName = "iPdf";
        public static readonly string IpcChannelName = "iPdf";
        public static readonly string HowToReportBugsFileName =  "How to Report Bugs Effectively.pdf";
        public static readonly string RemotingServiceName = "FirstInstance";
        public static readonly string IpcURL = "ipc://" + IpcChannelName + Helper.Slash + RemotingServiceName;
        private static MainForm mainWindow ;

        public static MainForm MainWindow
        {
            get
            {
                return mainWindow;
            }
            set
            {
                if (value != null)
                {
                    mainWindow = value;
                }
            }
        }

        public static TataRibbonPage CurrentPage
        {
            get
            {
                return mainWindow == null ? null : (TataRibbonPage)MainWindow.CurrentPage;
            }
        }

        public static string PdfConverterPath
        {
            get
            {
                string pdfConverter = "PdfConverter.dll";
                string pdfConverterTargetPath = Path.Combine( PdfCreator.GhostScriptBinDirectory, pdfConverter);
                string pdfConverterPath = Path.Combine(Application.StartupPath, pdfConverter);
                if (!File.Exists(pdfConverterTargetPath) || 
                    File.GetLastWriteTimeUtc(pdfConverterTargetPath) < 
                    File.GetLastWriteTimeUtc(pdfConverterPath))
                {
                    Helper.CopyFile(pdfConverterPath, pdfConverterTargetPath);
                }
                return pdfConverterTargetPath;
            }
        }

        public static TataGrid Grid
        {
            get
            {
                return mainWindow == null ? null : MainWindow.DataGrid;
            }
        }

        public static ApplicationMenu RibbonMenu
        {
            get
            {
                return mainWindow == null ? null : mainWindow.RibbonMenu;
            }
        }

        public static RecentFiles RecentFiles
        {
            get
            {
                return mainWindow == null? null : mainWindow.RecentFiles;
            }
        }

        public static void ShowNotification(string text)
        {
            if (mainWindow == null)
            {
                return;
            }

            mainWindow.ShowNotification(text);
        }

        public static void Cleanup()
        {
            if (mainWindow != null)
            {
                mainWindow.Cleanup();
            }
        }

        public static void CheckUserCancellation()
        {
            if (MainForm.ActionCancelled)
            {
                throw new ActionCancelled();
            }
        }

    }
}
